var miaouObject = function ( kind , id , posTop , posLeft, x, y , iWidth , iHeight) {
    
    var _this=this ; 
    this.kind = kind ;       /* The kind of cat */
    this.sId= id;            /* The object identifier */
    var objId      ;         /* The document object Id */
    var posTopPx  = posTop ; /* The top's position */
    var posLeftPx = posLeft ; /* The left's position */
    this.xPos = x;        /* The x's position in the game's table */
    this.yPos = y ;       /* The y's position in the game's table */   
    this.xOldPos=0 ;      /* The x's previous position */
    this.yOldPos=0 ;      /* The y's previous position */
    this.score = 0 ;      /* The miaou's score */
    this.direction = 0;   /* The miaou's automatic direction */
    this.t = 0 ;          /* Time out return */
    this.iWidth=iWidth;        /* Miaou's width */
    this.iHeight=iHeight;       /* Miaou's height */
    
   /**
     * The Yellow cat die , game over 
     **/
    this.die = function() {
        
        alert( " Vous êtes mort . Fin de de la partie ! ") ; 
        document.location.reload();
    }
    
    /**
     * Set the kind code at the current object's yPos and xPos.
     * 
     */
    this.setKindCode = function() {
       
       gameTable[this.yPos][this.xPos]=this.kind ;
    }
    
    /**
     * Get the current code at the current object's yPos and xPos.
     * 
     * @return int The current code
     */
    this.getCode = function() {
       
       return gameTable[this.yPos][this.xPos] ;
    }
    
    /**
     * Get the current code at the current object's yPos and xPos.
     * 
     * @return String The current code
     */
    this.getSCode = function() {
       
       return ""+gameTable[this.yPos][this.xPos] ;
    }
    
    
    /**
     * Update the miaou's position
     */
    this.updatePosition = function() {
       
       objId = document.getElementById(id); 
       objId.style.position="absolute";
       objId.style.top= posTopPx+"px" ; 
       objId.style.left=posLeftPx+"px";
       
       //Initialize old positions if always 0 after a move
       if ( this.xOldPos == 0) this.xOldPos=this.xPos;
       if ( this.yOldPos == 0) this.yOldPos=this.yPos;
       
       //Empty the cat's previous position
       gameTable[this.yOldPos][this.xOldPos] = EMPTY ; 
              
       //A cat meets a fish
       if ( isEatable(this.getCode()) == true ) this.eatAFish();
       //My Miaou meets another a lethal object
       else if (  this.kind == MY_MIAOU  && isLethal(this.getCode())==true ) this.die();
       //Bad cat meets My Miaou
       else if (   ( this.kind == BLACK_CAT  || this.kind == BLACK_WHITE_CAT ) 
                 &&  this.getCode() == MY_MIAOU  ) this.die();
       else if ( this.kind == MY_MIAOU  && isHomeAvailable == true
                 && this.getCode() == HOME  ) nextLevel();
       else if ( this.kind == MY_MIAOU  && this.getSCode().substring(0,1)==SHOE ) this.openSpecialWall();
       else this.setKindCode() ; 
       
       if ( this.kind == MY_MIAOU ) {
           
           var tmpxOldpos=this.xOldPos;
           var tmpyOldpos=this.yOldPos;
           var tmpxPos=this.xPos;
           var tmpyPos=this.yPos;
           
           dbug( "xOld="+tmpxOldpos+" "+"yOld="+tmpyOldpos+"<BR>"+
                 "x="+tmpxPos+" "+"y="+tmpyPos+"<BR>"+
                 "Old (xOld,yOld)="+gameTable[tmpyOldpos][tmpxOldpos]+"<BR>"+
                 "Curr  (x  ,y )="+gameTable[tmpyPos][tmpxPos]+"<BR>"+
                 "Left  (x-1,y )="+gameTable[tmpyPos][tmpxPos-1]+"<BR>"+
                 "Right (x+1,y )="+gameTable[tmpyPos][tmpxPos+1]+"<BR>"+
                 "Upd   (x  ,y-1)="+gameTable[tmpyPos-1][tmpxPos]+"<BR>"+
                 "Down  (x  ,y+1 )="+gameTable[tmpyPos+1][tmpxPos]+"<BR>"+
                 "Current speed="+currentSpeed+"<BR>"
                                       ) ;
       }
    }
    
    /**
     * Open the special wall
     */
    this.openSpecialWall = function() {
        
        //Retrieve the shoe number linked with the special wall
        var nbShoe = gameTable[this.yPos][this.xPos].substr(2); 
        //Clear the shoe place
        gameTable[this.yPos][this.xPos] = this.kind;
        document.getElementById("shoeId_"+nbShoe).innerHTML="";
        //Clear the special wall place
        var specialWallId=document.getElementById("specialWallId_"+nbShoe);
        specialWallId.innerHTML="";
        gameTable[specialWallId.getAttribute("yPos")][specialWallId.getAttribute("xPos")] = 0;
    }
    
    /**
     * Update the maiou's size 
     */
    this.updateSize = function() {
       objId              = document.getElementById(id); 
       objId.style.height = this.iHeight+"px";
       objId.style.width  = this.iWidth+"px";
       objId.style.overflow="auto";
       imgId= document.getElementById("img_"+id);
       imgId.style.height = this.iHeight+"px";
       imgId.style.width  = this.iWidth+"px";
    }
    
    /**
     * Miaou moves on the left side
     */
    this.moveLeft = function() {
        
        posLeftPx-=W ; 
        this.yOldPos=this.yPos ; 
        this.xOldPos=this.xPos ; 
        this.xPos=this.xPos-1; 
        this.updatePosition();
    }
    
    /**
     * Miaou moves on the right side
     */
    this.moveRight = function() {
        
        posLeftPx+=W ; 
        this.yOldPos=this.yPos ; 
        this.xOldPos=this.xPos ; 
        this.xPos=this.xPos+1; 
        this.updatePosition();
    }
    
    /**
     * Miaou moves on the top side
     */
    this.moveUp = function() {
        
        posTopPx-=H ; 
        this.yOldPos=this.yPos ; 
        this.xOldPos=this.xPos ; 
        this.yPos=this.yPos-1; 
        this.updatePosition();
    }
    
    /**
     * Miaou moves on the down side
     */
    this.moveDown = function() {
        
        posTopPx+=H ;
        this.yOldPos=this.yPos ; 
        this.xOldPos=this.xPos ; 
        this.yPos=this.yPos+1; 
        this.updatePosition();
    }
    
    /**
     * Miaou jumps over obstacles
     */
    this.jump = function() {
      
          for(i=1;i<10;i++) {
            this.iWidth+=5;
            this.iHeight+=5;
            this.updateSize();
            for(tempo=0;tempo<100000;tempo++) {a=1};
          }
    }
    
    
    /**
     * Miaou eats a fish
     *
     * Increase the miaou's score and delete the fish from the game table
     */
    this.eatAFish = function() {
        /* Increase the score */
        this.score+=1;
        /* Delete the fish from the game table */
        var fish = document.getElementById('fish_'+this.yPos+'_'+this.xPos);
        fish.innerHTML="";
        
        gameTable[this.yPos][this.xPos] = this.kind ;
        
        /* A meow sound */
        var snd=new Audio("./sounds/cat-meow.wav");
        snd.play();
        
        /* Increase the eaten fish number */
        nbEatFish++;
        if ( nbEatFish == nbTotalFish ) displayHome();
        
        /* Display the score (only for yellow cat) */
        if (  this.kind == MY_MIAOU ) {
            displayScore( this.score ) ;
        }
        /* Change the game's speed */
        changeSpeed() ;
   }
    
    /**
     * Miaou gets a new random direction 
     */
    this.getDirection = function() {
        
        /* Get an alea direction */
        /* The direction is good if the next case isn't a wall */
        var goodDirection = false ;
        var aleaDirection = 0 ;
        
        while ( goodDirection == false ) {
            aleaDirection = Math.round( 4 * Math.random() ) ; 
            /* 1=left */
            if ( aleaDirection == D_LEFT && this.isObstacle(this.yPos,this.xPos-1) == false ) goodDirection=true;
            /* 2=right */
            if ( aleaDirection == D_RIGHT && this.isObstacle(this.yPos,this.xPos+1) == false ) goodDirection=true;
            /* 3=up */
            if ( aleaDirection == D_UP && this.isObstacle(this.yPos-1,this.xPos) == false ) goodDirection=true;
            /* 4=down */
            if ( aleaDirection == D_DOWN && this.isObstacle(this.yPos+1,this.xPos) == false) goodDirection=true;
        }
        return aleaDirection;
    }
    
    /**
     * Miaou can moves if there's no obstacle
     * on the next case
     */
    this.canMove = function() {
        
        var cMove=false ; 
        
        /* 1=left */
        if ( this.direction == D_LEFT && this.isObstacle( this.yPos,this.xPos-1) == false ) cMove=true ; 
        /* 2=right */
        if ( this.direction == D_RIGHT && this.isObstacle(this.yPos,this.xPos+1) == false ) cMove=true ;
        /* 3=up */
        if ( this.direction == D_UP && this.isObstacle(this.yPos-1,this.xPos) == false) cMove=true ;
        /* 4=down */
        if ( this.direction == D_DOWN && this.isObstacle(this.yPos+1,this.xPos) == false) cMove=true
     
        return cMove; 
    } 
    
    /**
     * Test if a case is an obstacle or not
     * @param int y coordonate
     * @param int x coordonate
     * 
     * @return boolean True if the case is an obstacle for the cat
     *                 False otherwise
     */
    this.isObstacle = function( yPos , xPos ) {
        
        var iCode = gameTable[yPos][xPos] ;
        var sCode = ""+iCode ; 
        var bObstacle=false ; 
        
        if ( iCode == WALL) bObstacle=true;
        if ( sCode.substring(0,2) == SPECIAL_WALL) bObstacle=true;
        
        if ( this.kind != MY_MIAOU && iCode==HOME ) bObstacle=true;
        if ( this.kind != MY_MIAOU && sCode.substring(0,1)==SHOE ) bObstacle=true;

        return bObstacle;
    }
    
    /**
     * The cat is near something (a fish or a cat)
     * so it changes the direction
     * 
     * @return boolean/integer By default, no change of direction
     */
    this.nearSomethingToEat = function() {
        
        var newDirection=false ; 
        /* Something at left */
        if ( ( isEatable(gameTable[this.yPos][this.xPos-1])==true ||
               isMyMiaou(gameTable[this.yPos][this.xPos-1])==true ) 
               && this.isObstacle(this.yPos,this.xPos-1)== false ) newDirection = D_LEFT ;
        
        /* Something at right */
        if ( ( isEatable(gameTable[this.yPos][this.xPos+1])==true ||
               isMyMiaou(gameTable[this.yPos][this.xPos+1])==true ) 
               && this.isObstacle(this.yPos,this.xPos+1)==false ) newDirection = D_RIGHT ;
        
        /* Something up */
        if ( ( isEatable(gameTable[this.yPos-1][this.xPos])==true ||
               isMyMiaou(gameTable[this.yPos-1][this.xPos])==true ) 
               && this.isObstacle(this.yPos-1,this.xPos)==false ) newDirection = D_UP ;
        
        /* Something down */
        if ( ( isEatable(gameTable[this.yPos+1][this.xPos])==true ||
               isMyMiaou(gameTable[this.yPos+1][this.xPos])==true ) 
               && this.isObstacle(this.yPos+1,this.xPos)==false ) newDirection = D_DOWN ; 
        
        return newDirection ; 
    }
    
    /**
     * Miaou automatically moves
     * It choose a good direction with no wall and move
     */
    this.autoMove = function() {
        
        /* No direction set or can't moves */ 
        var cMove= this.canMove();
        if ( cMove == false ) {
            this.direction = this.getDirection();  
        }

        /* Is the cat is near something to eat, so its changes his direction */
        if ( this.nearSomethingToEat() != false )  this.direction = this.nearSomethingToEat() ; 
        
        switch ( this.direction ) {
            case D_LEFT :
                this.moveLeft();
            break;
            case D_RIGHT :
                this.moveRight();
            break;
            case D_UP :
                this.moveUp();
            break;
            case D_DOWN :
                this.moveDown();
            break;
        } 
    }
}